package com.lrs.java.jdbc.jdbc_teacher;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//插入,使用PreparedStatement
class TestInsertEmp2Prep {
	public static void main(String[] args) {
		if (args.length != 7) {
			System.out.println("args length error!");
			System.exit(-1);
		}

		String strEmpNo = args[0];
		int empno = -1;
		try {
			empno = Integer.parseInt(strEmpNo);
		} catch (NumberFormatException e1) {
			System.out.println("empno error");
			e1.printStackTrace();
			System.exit(-1);
		}

		String ename = args[1];
		String job = args[2];
		int mgr = Integer.parseInt(args[3]);
		double sal = Double.parseDouble(args[4]);
		double comm = Double.parseDouble(args[5]);
		int deptno = Integer.parseInt(args[6]);

		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@192.168.0.1:1521:SXTBJ", "scott",
					"tiger");
			//什么时候用prepareStatement?
			//1,需要执行大量重复的语句
			//2,sql字符串不好拼的时候
			pstmt = conn.prepareStatement("insert into emp2 values (?, ? ,?, ? , sysdate, ?, ?, ?)");

			pstmt.setInt(1, empno);
			pstmt.setString(2, ename);
			pstmt.setString(3, job);
			pstmt.setInt(4, mgr);
			pstmt.setDouble(5, sal);
			pstmt.setDouble(6, comm);
			pstmt.setInt(7, deptno);
			pstmt.executeUpdate();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
					pstmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
